<template>
<div>
   <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item label="实验名称">
        <el-input v-model="dataForm.experimentName" placeholder="实验名称" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary"  plain size="mini" @click="getDataList()">查询</el-button>
        <el-button  type="success"  plain size="mini"  @click="addHandle()">新增</el-button>
        <el-button  type="danger"  plain size="mini" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      :default-sort = "{order: 'descending'}"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="id"
        type="index"
        fixed="left"
        header-align="center"
        align="center"
        width="80"
        label="ID">
      </el-table-column>
      <el-table-column
        prop="experimentName"
        header-align="center"
        width="180"
        fixed="left"
        align="center"
        label="实验名称">
      </el-table-column>
      <el-table-column
        prop="task"
        header-align="center"
        align="center"
        width="180"
        show-overflow-tooltip
        label="任务内容">
      </el-table-column>
      <el-table-column
        prop="file"
        header-align="center"
        align="center"
        width="150px"
        label="实验资料">
        <template slot-scope="scope">
          <a :href="scope.row.file" v-if="scope.row.file !== ''" >
            <el-tag>资料下载</el-tag>
          </a>
        </template>
      </el-table-column>
      <el-table-column
        prop="startTime"
        header-align="center"
        align="center"
        width="180"
        sortable
        label="开始时间">
      </el-table-column>
      <el-table-column
        prop="endTime"
        header-align="center"
        align="center"
        width="180"
        sortable
        label="结束时间">
      </el-table-column>
      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        width="180"
        sortable
        label="创建时间">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="250"
        label="操作">
        <template slot-scope="scope">
          <el-button  type="success" plain size="mini" @click="updateHandle(scope.row.id)">修改</el-button>
          <el-button type="danger"  plain size="mini" @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[9, 18, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗,修改 -->
    <task-update v-if="addOrUpdateVisible" ref="updateHandle" @refreshDataList="getDataList"></task-update>

  </div>
</template>

<script>
  /* eslint-disable */
  import TaskUpdate from './task-update.vue'

export default {
  components: {TaskUpdate},
    data () {
      return {
        dataForm: {
          experimentName: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 9,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false
      }
    },
    computed: {
      id: {
        get () { return this.$store.state.user.id },
        set (val) { this.$store.commit('user/updateId', val) }
      },
      account: {
        get () { return this.$store.state.user.account },
        set (val) { this.$store.commit('user/updateAccount', val) }
      },
      avatar: {
        get () { return this.$store.state.user.avatar },
        set (val) { this.$store.commit('user/updateAvatar', val) }
      }
    },
    activated () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/task/getTaskPage'),
          method: 'post',
          data: this.$http.adornData({
            'experimentName': this.dataForm.experimentName,
            'account': this.account,
            'pageNo': this.pageIndex,
            'pageSize': this.pageSize
          })
        }).then(({data}) => {
          if (data.success) {
            this.dataList = data.data.records
            this.totalPage = data.data.total
          } else {
            this.dataList = []
            this.totalPage = 0
            this.$message.error(data.message)
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      addHandle (){
        this.$router.push("/task-publish")
      },
      viewHandle (id){
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.viewHandle.init(id)
        })
      },
      updateHandle (id){
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.updateHandle.init(id)
        })
      },
      // 删除
      deleteHandle (id) {
        var userIds = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        console.log("userIds: ",userIds)
        this.$confirm(`确定要进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/task/deleteTask'),
            method: 'post',
            data: this.$http.adornData({
              'ids': userIds,
              'updateBy': this.$store.state.user.account
            })
          }).then(({data}) => {
            if (data.success) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.message)
            }
          })
        }).catch(() => {})
      }
    }
  }
</script>
<!-- <style lang="scss" scoped>
 .el-button--primary {
      color: #fff;
      background-color: #273471 !important;
      border-color: #273471 !important;
  }
</style> -->
